package cs236369.webserver;

import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.Properties;

import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.entity.FileEntity;
import org.apache.http.impl.DefaultHttpServerConnection;
import org.apache.http.protocol.HttpContext;

public class DefaultTypeHandler implements ITypeHandler{
	
	private String mPath;
	private DefaultHttpServerConnection mConnToClient;
	
	@Override
	public void handle(HttpRequest request, HttpResponse response, HttpContext context)
			throws HttpException, IOException {
		
        response.setStatusCode(HttpStatus.SC_OK);
        FileEntity body = new FileEntity(new File(mPath),"");
        response.setEntity(body);
         
		String contentLength = Long.toString(response.getEntity().getContentLength());
		response.addHeader("Content-length",contentLength);
        
		mConnToClient.sendResponseHeader(response);
		mConnToClient.sendResponseEntity(response);
		
	}
	
	public DefaultTypeHandler(Properties pr) {
		
	}

	
	@Override
	public void init(String path, DefaultHttpServerConnection connToClient, Map<String, String> httpParams) {
		mPath = path;
		mConnToClient = connToClient;
		
	}

}
